﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nextropia.C2Manager.C2API
{
    /// <summary>
    /// represents a simple string name and value pair used for passing parameters
    /// </summary>
    public class NameValuePair
    {
        /// <summary>
        /// the name of the parameter
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// the value of the parameter
        /// </summary>
        public string Value { get; set; }

        /// <summary>
        /// default constructor
        /// </summary>
        public NameValuePair()
        {
        }

        /// <summary>
        /// constructor with string name and string value parameters
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public NameValuePair(string name, string value)
        {
            Name = name;
            Value = value;
        }

        /// <summary>
        /// constructor with string name and object value parameters; value.ToString() is used to obtain the string value
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public NameValuePair(string name, object value)
        {
            Name = name;

            if  (value != null)
                Value = value.ToString();
        }

        /// <summary>
        /// converts a string[] to a NameValuePair object implicitly
        /// </summary>
        /// <param name="nameAndValue"></param>
        /// <returns></returns>
        public static implicit operator NameValuePair(string[] nameAndValue)
        {
            string name = null, value = null;

            if (nameAndValue.Length > 0)
                name = nameAndValue[0];

            if (nameAndValue.Length > 1)
                value = nameAndValue[1];

            return new NameValuePair { Name = name, Value = value };
        }
    }
}
